package mge.ui.controls
{
  import flash.display.MovieClip;
  import flash.events.Event;
  import flash.events.MouseEvent;
  import flash.text.TextField;
  import mge.ui.events.ButtonEvent;
  
	public class Button extends MovieClip
	{
    private var _label:String;
    public var textField:TextField; // name of the TextField instance in editor.
    
    [Inspectable(defaultValue="")]
    public function get label():String { return _label; }
    public function set label(value:String):void { textField.text = _label = value; }
    
    [Inspectable(defaultValue="true")]
    override public function get enabled():Boolean { return super.enabled; }
    override public function set enabled(value:Boolean):void 
    {
        super.enabled = value;
        mouseChildren = false;
        gotoAndPlay(super.enabled ? "up" : "disabled");
    }
    
    [Inspectable(defaultValue="true")]
    public function get selected():Boolean { return _selected; }
    public function set selected(value:Boolean):void 
    {
        _selected = value;
        gotoAndPlay(_selected ? "selected" : "up");
    }
    public var _selected:Boolean;
    
    
        
		public function Button()
		{
      trace("Button");
      super();
      
      addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
      addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
      addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
      addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
      
      this.buttonMode = true;
      
      gotoAndPlay("up");
		}
    
    public function onMouseOver(e:Event):void
    {
      if (!enabled) { return; }
      gotoAndPlay(_selected ? "selected" : "over");
    }
    
    public function onMouseOut(e:Event):void
    {
      if (!enabled) { return; }
      gotoAndPlay(_selected ? "selected" : "up");
    }
    
    private function onMouseUp(e:Event):void
    {
      if (!enabled) { return; }
      
      onMouseOver(e);
      var event:ButtonEvent = new ButtonEvent(ButtonEvent.CLICK);
      dispatchEvent(event);
    }
    
    public function onMouseDown(e:Event):void
    {
      if (!enabled) { return; }
      gotoAndPlay("down");
    }
	}
}